package cn.ratopia.core.security.auth;

import cn.ratopia.core.model.LoginUser;
import cn.ratopia.core.security.service.TokenService;
import io.micrometer.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class WsAuthService {
    @Autowired
    private TokenService tokenService;

    public LoginUser authenticate(String token) {
        if (StringUtils.isBlank(token)) {
            return null;
        }

        try {
            LoginUser user = tokenService.getLoginUser(token);
            if (user != null) {
                tokenService.verifyToken(user); // 自动续期
            }
            return user;
        } catch (Exception e) {
            return null;
        }
    }
}
